using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using FoxCommerce.MVC.Domain.Entities;

namespace FoxCommerce.MVC.Data
{
    public class FoxCommerceMVCWebContext : DbContext
    {
        public FoxCommerceMVCWebContext()
            : base("FoxCommerceMVC")
        {

        }
        public virtual void Commit()
        {
            base.SaveChanges();
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<User>()
           .HasMany(u => u.Roles)
           .WithMany(r => r.Users)
           .Map(m =>
           {
               m.ToTable("RoleMemberships");
               m.MapLeftKey("UserName");
               m.MapRightKey("RoleName");
           });
        }

        public class FoxCommerceMVCWebContextInitializer : DropCreateDatabaseIfModelChanges<FoxCommerceMVCWebContext>
        {
            protected override void Seed(FoxCommerceMVCWebContext context)
            {
                var roles = new List<Role>{
                new Role{RoleName = "Administrator"},
                new Role{RoleName = "User"}               
            };

                roles.ForEach(r => context.Roles.Add(r));
            }
        }
        public DbSet<User> Users { get; set; }
        public DbSet<Role> Roles { get; set; }

        public DbSet<Product> Products { get; set; }

        public DbSet<Category> Categories { get; set; }

        public DbSet<CategoryAttribute> CategoryAttributes { get; set; }

        public DbSet<Discount> Discounts { get; set; }

        public DbSet<Picture> Pictures { get; set; }
    }
}